/********************************************************/
/*	Author:		gong_libin			*/
/*	Date:		2014_08_08			*/
/*	File:		GtRmUtil.h			*/
/********************************************************/

#ifndef	_GTRMUTIL_H
#define	_GTRMUTIL_H

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include "GtGlobal.h"
#include <fcntl.h>
#include <ctype.h>
#include <getopt.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include <net/if.h>
#include <sys/un.h>
#include <sys/shm.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#define __FAVOR_BSD
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <net/ethernet.h>
#include <netpacket/packet.h>

/* raw */
#define	GT_RU_BUF			0x00100000

int GtRuRawInitRecv(char* pszEth);
int GtRuRawInitSend(char* pszAddr);

/* shm */
#define GT_RU_KEY                       'G'
#define GT_RU_PATH                      (char*)"/root"

typedef struct tagGtRuShm
{
	ULONGLONG m_ullSend;
	ULONGLONG m_ullRecv;
	ULONGLONG m_ullBytes;
}GTRUSHM_S;

void* GtRuShmCreat(int bReset);

/* conf */
#define	GT_RU_TTL			16

#define	GT_RU_POT			(char*)"PORT"
#define	GT_RU_ETH			(char*)"ETHER"

#define	GT_RU_MDSC			pstConf->m_stDevice[iMod].m_iSock
#define	GT_RU_MDSZ			pstConf->m_stDevice[iMod].m_iAddr
#define	GT_RU_MDIN			pstConf->m_stDevice[iMod].m_unAddr.m_stIn
#define	GT_RU_MDUN			pstConf->m_stDevice[iMod].m_unAddr.m_stUn

#define	GT_RU_ADSC			pstConf->m_stDevice[pstConf->m_uiDevice].m_iSock
#define	GT_RU_ADSZ			pstConf->m_stDevice[pstConf->m_uiDevice].m_iAddr
#define	GT_RU_ADIN			pstConf->m_stDevice[pstConf->m_uiDevice].m_unAddr.m_stIn
#define	GT_RU_ADUN			pstConf->m_stDevice[pstConf->m_uiDevice].m_unAddr.m_stUn

#define	GT_RU_MDAD			(struct sockaddr*)&pstConf->m_stDevice[iMod].m_unAddr
#define	GT_RU_ADAD			(struct sockaddr*)&pstConf->m_stDevice[pstConf->m_uiDevice].m_unAddr

typedef struct tagGtRuConf
{
	char m_szPort[GT_BYTE128];
	char m_szEther[GT_BYTE128];

	UINT m_uiDevice;
	struct {
		int m_iSock;
		int m_iAddr;
		union {
			struct sockaddr_in m_stIn;
			struct sockaddr_un m_stUn;
		} m_unAddr;
	} m_stDevice[GT_RU_TTL];
}GTRUCONF_S;

int GtRuAddrIn(char* pszLine, GTRUCONF_S* pstConf);
int GtRuAddrUn(char* pszLine, GTRUCONF_S* pstConf);
int GtRuKeyValue(char* pszLine, GTRUCONF_S* pstConf);
void GtRuConfPrint(char* pszBlock, GTRUCONF_S* pstConf);
int GtRuConfigure(char* pszConf, char* pszBlock, GTRUCONF_S* pstConf);

/* util */
void GtRuFree(void** ppFree);
void* GtRuMalloc(UINT uiSize);
void GtRuTrimAll(char* pszLine);
void GtRuTrimLeft(char* pszLine);
void GtRuTrimRight(char* pszLine);
void GtRuTimer(struct timeval* pstOn, struct timeval* pstOff, char* pszTime);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _GTRMUTIL_H */
